package com.jiutong.meipengdai.ui.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.utils.TimerUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作用:
 * Created by 78 on 2017/6/28.
 */
public class EmailChangeActivity extends BaseActivity implements RegisterAndLoginBiz {
    @Bind(R.id.rl_back)
    RelativeLayout rl_back;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_options)
    TextView tvOptions;
    @Bind(R.id.rl_comment_title)
    RelativeLayout rlCommentTitle;
    @Bind(R.id.view_line)
    View viewLine;
    @Bind(R.id.tv_email_change_phonenum)
    TextView tvEmailChangePhonenum;
    @Bind(R.id.et_email_change_identify_code)
    EditText etEmailChangeIdentifyCode;
    @Bind(R.id.tv_email_change_getcode)
    TextView tvEmailChangeGetcode;
    @Bind(R.id.btn_email_change_next)
    Button btnEmailChangeNext;


    private UserInfoPresent userInfoPresent;
    private String identifyCode;
    private TimerUtil timerUtil;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_email_change;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("更换邮箱");

        Bundle bundle = getIntent().getExtras();
        if(bundle==null){
            return;
        }
        tvEmailChangePhonenum.setText(bundle.getString("phone"));

        //设置未填写正确按钮状态
        btnEmailChangeNext.setEnabled(false);
        etEmailChangeIdentifyCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(getVerificatCode().length()!=6){
                    btnEmailChangeNext.setBackgroundResource(R.drawable.shape_light_gray_btn);
                    btnEmailChangeNext.setEnabled(false);
                }else{
                    btnEmailChangeNext.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                    btnEmailChangeNext.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //初始化倒计时
        timerUtil = new TimerUtil(tvEmailChangeGetcode);
        timerUtil.setCountTimer();

        userInfoPresent = new UserInfoPresent(this, this);
    }

    @OnClick({R.id.rl_back, R.id.tv_email_change_getcode, R.id.btn_email_change_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_email_change_getcode:
                userInfoPresent.getAuthCode();
                break;
            case R.id.btn_email_change_next:
                if(isRightAction())
                    openActivity(EmailBinddingActivity.class);
                break;
        }
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {

        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入手机验证码");
            return false;
        }
        if (!identifyCode.equals(getVerificatCode())) {
            toastShortShow("您输入的验证码错误，请重新输入");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return tvEmailChangePhonenum.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getVerificatCode() {
        return etEmailChangeIdentifyCode.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {
        if(subjects.getResCode().equals("200")){
            timerUtil.startTimer();
            identifyCode = subjects.getData();
        }else{
            toastShortShow(subjects.getResDesc());
        }
    }

    @Override
    public void onSuccess(Object object) {

    }

    @Override
    public void onError(Throwable e) {

    }
}
